package com.foodorder.controller;

import com.foodorder.entity.Review;
import com.foodorder.entity.Review.ReviewType;
import com.foodorder.model.ApiResponse;
import com.foodorder.model.PageResult;
import com.foodorder.model.request.ReviewAddRequest;
import com.foodorder.model.request.ReviewQueryRequest;
import com.foodorder.model.request.ReviewReplyRequest;
import com.foodorder.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 评价管理控制器
 */
@RestController
@RequestMapping("/api/reviews")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 添加评价
     */
    @PostMapping
    public ResponseEntity<ApiResponse> addReview(@RequestBody ReviewAddRequest request) {
        Review review = reviewService.addReview(request);
        return ResponseEntity.ok(ApiResponse.success(review));
    }

    /**
     * 获取评价详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse> getReview(@PathVariable Long id) {
        Review review = reviewService.getReviewById(id);
        return ResponseEntity.ok(ApiResponse.success(review));
    }

    /**
     * 分页获取评价列表
     */
    @GetMapping
    public ResponseEntity<ApiResponse> getReviews(ReviewQueryRequest request) {
        PageResult<Review> reviews = reviewService.getReviews(request);
        return ResponseEntity.ok(ApiResponse.success(reviews));
    }

    /**
     * 获取商品评价列表
     */
    @GetMapping("/product/{productId}")
    public ResponseEntity<ApiResponse> getProductReviews(
            @PathVariable Long productId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        ReviewQueryRequest request = new ReviewQueryRequest();
        request.setProductId(productId);
        request.setType(ReviewType.PRODUCT);
        request.setPage(page);
        request.setSize(size);
        
        PageResult<Review> reviews = reviewService.getReviews(request);
        return ResponseEntity.ok(ApiResponse.success(reviews));
    }

    /**
     * 获取门店评价列表
     */
    @GetMapping("/store/{storeId}")
    public ResponseEntity<ApiResponse> getStoreReviews(
            @PathVariable Long storeId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        ReviewQueryRequest request = new ReviewQueryRequest();
        request.setStoreId(storeId);
        request.setPage(page);
        request.setSize(size);
        
        PageResult<Review> reviews = reviewService.getReviews(request);
        return ResponseEntity.ok(ApiResponse.success(reviews));
    }

    /**
     * 获取用户评价列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<ApiResponse> getUserReviews(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        ReviewQueryRequest request = new ReviewQueryRequest();
        request.setUserId(userId);
        request.setPage(page);
        request.setSize(size);
        
        PageResult<Review> reviews = reviewService.getReviews(request);
        return ResponseEntity.ok(ApiResponse.success(reviews));
    }

    /**
     * 商家回复评价
     */
    @PostMapping("/{id}/reply")
    public ResponseEntity<ApiResponse> replyReview(
            @PathVariable Long id,
            @RequestBody ReviewReplyRequest request) {
        Review review = reviewService.replyReview(id, request.getReply());
        return ResponseEntity.ok(ApiResponse.success(review));
    }

    /**
     * 点赞评价
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<ApiResponse> likeReview(@PathVariable Long id) {
        Review review = reviewService.likeReview(id);
        return ResponseEntity.ok(ApiResponse.success(review));
    }

    /**
     * 获取评价统计信息
     */
    @GetMapping("/statistics/product/{productId}")
    public ResponseEntity<ApiResponse> getProductReviewStatistics(@PathVariable Long productId) {
        Map<String, Object> statistics = reviewService.getProductReviewStatistics(productId);
        return ResponseEntity.ok(ApiResponse.success(statistics));
    }

    /**
     * 获取门店评价统计信息
     */
    @GetMapping("/statistics/store/{storeId}")
    public ResponseEntity<ApiResponse> getStoreReviewStatistics(@PathVariable Long storeId) {
        Map<String, Object> statistics = reviewService.getStoreReviewStatistics(storeId);
        return ResponseEntity.ok(ApiResponse.success(statistics));
    }
} 